<?php


namespace app\store\service\lottery;


use app\store\model\OrderAddress as OrderAddressModel;

class Export
{
    /**
     * 表格标题
     * @var array
     */
    private $tileArray = [
        '订单号', '商品名称', '商品ID', '中奖者昵称', '中奖者ID', '收货人姓名', '联系电话', '收货人地址', '物流公司', '物流单号',
        '发货状态', '发货时间', '收货状态', '收货时间', '订单状态'
    ];

    /**
     * 订单导出
     * @param $list
     */
    public function orderList($list)
    {
        // 表格内容
        $dataArray = [];
        foreach ($list as $order) {
            $dataArray[] = [
                '订单号' => $this->filterValue($order['order_no']),
                '商品名称' => $this->filterValue($order['goods']['goods_name']),
                '商品ID' => $this->filterValue($order['goods_id']),
                '中奖者昵称' => $this->filterValue($order['user']['nickName']),
                '中奖者ID' => $this->filterValue($order['user_id']),
                '收货人姓名' => $this->filterValue($order['username']),
                '联系电话' => $this->filterValue($order['phone']),
                '收货人地址' => $this->filterValue($order['address']),
                '物流公司' => $this->filterValue($order['express_name']),
                '物流单号' => $this->filterValue($order['express_no']),
                '发货状态' => $this->filterValue($order['order_status']['value'] >= 2 ? '已发货' : '代发货'),
                '发货时间' => $this->filterTime($order['delivery_time']),
                '收货状态' => $this->filterValue($order['order_status']['value'] >= 3 ? '已收货' : '代收货'),
                '收货时间' => $this->filterTime($order['receipt_time']),
                '订单状态' => $this->filterValue($order['order_status']['text'])
            ];
        }
        // 导出csv文件
        $filename = 'order-' . date('YmdHis');
        return export_excel($filename . '.csv', $this->tileArray, $dataArray);
    }

    /**
     * 表格值过滤
     * @param $value
     * @return string
     */
    private function filterValue($value)
    {
        return "\t" . $value . "\t";
    }

    /**
     * 日期值过滤
     * @param $value
     * @return string
     */
    private function filterTime($value)
    {
        if (!$value) return '';
        return $this->filterValue(date('Y-m-d H:i:s', $value));
    }
}